from src.core.config.napcat import *
from ..sender.napcatsender import NapcatSender
import json

"""
NapCat通讯类
使用http协议与NapCat服务器进行通信
支持私聊和群聊消息发送
"""
class NapCatBase:
    _instance = None  # 单例实例

    def __new__(cls, url=None, port=None):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance.sender = NapcatSender(url, port)
        return cls._instance

    def send_private_msg(self, msg, id):
        payload = json.dumps({
            "user_id": id,
            "message": [
                {
                    "type": "text",
                    "data": {"text": msg}
                }
            ]
        })
        return self.sender.send("/send_private_msg", payload)

    def send_group_msg(self, msg, group_id):
        payload = json.dumps({
            "group_id": group_id,
            "message": [
                {
                    "type": "text",
                    "data": {"text": msg}
                }
            ]
        })
        return self.sender.send("/send_group_msg", payload)

    def hello_to_manager(self):
        self.send_private_msg(MANAGER_HELLO, MANAGER_ID)
